package com.rambo.baselib.util;

import com.rambo.baselib.BaseApp;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

/***********************************************************************
 * <p>@description:
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/19 16:36
 **********************************************************************/
public class PrefUtil {

    private static final String PREF_NAME = "i_harmony";
    private DatabaseHelper databaseHelper;
    private Preferences preferences;

    private PrefUtil() {
        databaseHelper = new DatabaseHelper(BaseApp.getApp());
        preferences = databaseHelper.getPreferences(PREF_NAME);
    }

    private static class SingleHolder {
        private static PrefUtil INSTANCE = new PrefUtil();
    }

    public static PrefUtil getInstance() {
        return SingleHolder.INSTANCE;
    }

    public void write(String key, String value) {
        preferences.putString(key, value).flush();
    }

    public String getString(String key) {
        return preferences.getString(key, "");
    }

    public void write(String key, int value) {
        preferences.putInt(key, value).flush();
    }

    public int getInt(String key) {
        return preferences.getInt(key, 0);
    }

    public void write(String key, boolean value) {
        preferences.putBoolean(key, value).flush();
    }

    public boolean getBoolean(String key) {
        return preferences.getBoolean(key, false);
    }

    public void write(String key, float value) {
        preferences.putFloat(key, value).flush();
    }

    public void write(String key, long value) {
        preferences.putLong(key, value).flush();
    }

    public long getLong(String key) {
        return preferences.getLong(key, 0L);
    }
}
